using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;

namespace CodRevit.Commands
{
    [Transaction(TransactionMode.Manual)]
    public class ExportFamilyCommand : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                commandData.Application.Application.FailuresProcessing += FailuresProcessing;
                commandData.Application.DialogBoxShowing += DialogBoxShowingHandler;

                var window = new Views.ExportFamilyView();
                window.DataContext = new ViewModels.ExportFamilyViewModel(commandData.Application.ActiveUIDocument);
                window.ShowDialog();

                commandData.Application.Application.FailuresProcessing -= FailuresProcessing;
                commandData.Application.DialogBoxShowing -= DialogBoxShowingHandler;

                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return Result.Failed;
            }
        }

        private void DialogBoxShowingHandler(object sender, Autodesk.Revit.UI.Events.DialogBoxShowingEventArgs args)
        {
            args.OverrideResult(0);
        }

        private void FailuresProcessing(object sender, Autodesk.Revit.DB.Events.FailuresProcessingEventArgs args)
        {
            var failuresAccessor = args.GetFailuresAccessor();
            failuresAccessor.DeleteAllWarnings();
            args.SetProcessingResult(FailureProcessingResult.Continue);
        }
    }
}